package org.lanqiao.webfm.dao;

import java.util.*;
import java.io.Serializable;
import java.sql.*;
import java.lang.reflect.*;

import javacommon.base.*;
import javacommon.ijdbc.*;
import javacommon.util.*;
import javacommon.web.page.*;

import org.lanqiao.webfm.model.*;

import org.slf4j.*;

/**
*@Author zhengwei
**/
public class OrdersDetailDao extends BaseJdbcDao<OrdersDetail>{
	private static final Logger LOGGER = LoggerFactory.getLogger(OrdersDetailDao.class);
	private OrdersDetailDao(){}
	private static OrdersDetailDao instance = new OrdersDetailDao();
	public static OrdersDetailDao getInstance(){
		return instance;
	}
	public Class getEntityClass() {
		return OrdersDetail.class;
	}
	
	/*public RowMapper<OrdersDetail> getMapper() {
		return new OrdersDetailMapper();
	}*/
	public String getIdColumnName() {
		return "ID";
	}
	public String getTableName() {
		return "SP_ORDERS_DETAIL";
	}
	
	public OrdersDetail getById(Serializable id) throws SQLException {
		String sql = "select * from SP_ORDERS_DETAIL t1 "
				+ "where t1.ID=?";
        return operation.queryForUniqueBean(sql, getEntityClass(), id);
	}
	@Override
	public void save(OrdersDetail entity) throws SQLException {
		StringBuilder sql = new StringBuilder("insert into SP_ORDERS_DETAIL")
				.append(" (")
				.append("ID").append(",")
				.append("RNUM").append(",")
				.append("ORDER_ID").append(",")
				.append("GOODS_ID").append(",")
				.append("PRICE").append(",")
				.append("QTY")
				.append(") ")
				.append("values(dev_seq.nextval,")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?")
				.append(")");
			
		super.operation
				.execute(sql.toString(), 
								entity.getRnum(),
								entity.getOrderId(),
								entity.getGoodsId(),
								entity.getPrice(),
								entity.getQty()
				);
	}
	@Override
	public void update(OrdersDetail entity) throws SQLException {
		StringBuilder sql = new StringBuilder("update SP_ORDERS_DETAIL")
				.append(" set ") 
				.append("RNUM=?").append(",")
				.append("ORDER_ID=?").append(",")
				.append("GOODS_ID=?").append(",")
				.append("PRICE=?").append(",")
				.append("QTY=?")
				.append(" where ID=?");
		super.operation
				.execute(sql.toString(),
							entity.getRnum(),
							entity.getOrderId(),
							entity.getGoodsId(),
							entity.getPrice(),
							entity.getQty(),
						entity.getId());
	}
	/**
	 * 根据query对象来查询数据，结果取决于query中有查询条件及query中的分页信息
	 */
	public Page<OrdersDetail> findPage(PageRequest<OrdersDetail> query) throws SQLException{
		OrdersDetail ordersDetail = (OrdersDetail) query;
        StringBuilder sql = new StringBuilder("select t.* from SP_ORDERS_DETAIL t where 1=1 ");
        if(StringUtils.hasLength(ordersDetail.getId())) {
         	sql.append(" and  t.ID =  ").append(ordersDetail.getId());
        }
        if(StringUtils.hasLength(ordersDetail.getRnum())) {
         	sql.append(" and  t.RNUM =  ").append(ordersDetail.getRnum());
        }
        if(StringUtils.hasLength(ordersDetail.getOrderId())) {
         	sql.append(" and  t.ORDER_ID =  ").append(ordersDetail.getOrderId());
        }
        if(StringUtils.hasLength(ordersDetail.getGoodsId())) {
         	sql.append(" and  t.GOODS_ID =  ").append(ordersDetail.getGoodsId());
        }
        if(StringUtils.hasLength(ordersDetail.getPrice())) {
         	sql.append(" and  t.PRICE =  ").append(ordersDetail.getPrice());
        }
        if(StringUtils.hasLength(ordersDetail.getQty())) {
         	sql.append(" and  t.QTY =  ").append(ordersDetail.getQty());
        }
        if(StringUtils.hasLength(ordersDetail.getSortColumns())) {
            sql.append(" order by ").append(ordersDetail.getSortColumns());
        }	
        
		return pageQuery(sql.toString(),ordersDetail);
	}
	
	public void deleteByIds(String[] ids) throws SQLException {
		String[] sqls = new String[ids.length];
		for (int i = 0; i < sqls.length; i++) {
			sqls[i] = "delete from SP_ORDERS_DETAIL where ID=" + ids[i];
		}
		operation.executeBatch(sqls);
	}

}
